package mianxiangduixiang.Test7;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author JetZhang
 * Package:mianxiangduixiang.Test7
 * Description:生成十个Book对象存入book2.dat  再从book2.dat复制到book3.dat
 * @Date 2021/11/18
 */
public class T3 {
    public static void main(String[] args) {
        try {
            FileOutputStream fOut = new FileOutputStream("book2.dat");
            ObjectOutputStream out = new ObjectOutputStream(fOut);
            List<Book> list = new ArrayList<>();

            for (int i = 1; i <= 10; i++) {
                Book book = new Book("测试" + i, "测试" + i + "_" + i, "测试" + i + "_" + i + "_" + i);
                list.add(book);
            }

            out.writeObject(list);
            out.close();

            ObjectInputStream oIn = new ObjectInputStream(new FileInputStream("book2.dat"));
            Object obj = oIn.readObject();

            ObjectOutputStream oOut = new ObjectOutputStream(new FileOutputStream("book3.dat"));
            if (obj != null) {
                oOut.writeObject(obj);
                System.out.println(obj);
                oOut.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
